using Super;
using System;
using System.Collections.Generic;
using UnityEngine;

public class AABBController : MonoBehaviour
{
	public GameObject prefab;
	public Transform parent;
	public int count = 0;
	public Vector3 minPoint;
	public Vector3 maxPoint;
	public Action<GameObject> GenerateCallBack;
	public GOPool pool;
	public MultiTree<AABB> tree = new MultiTree<AABB>();
	private Vector3 downPoint = Vector3.zero;
	private Vector3 upPoint = Vector3.zero;
	private Bounds bounds = new Bounds();
	private bool isMouseDown = false;
	private Plane plane;
	void Start()
	{
		plane = new Plane(Vector3.forward, parent.transform.position);
		Generate();
		for (int i = 0; i < 4; i++)
		{
			SetColor(i, GlobalUtil.GetColor(i * 3));
		}
	}

	public void Generate()
	{
		if (pool == null)
			pool = new GOPool(prefab, parent);
		pool.RecycleAll();
		GameObject go;
		Vector3 offsetPos, pos;
		Vector3 basePos = parent != null ? parent.transform.position : Vector3.zero;

		for (int i = 0; i < count; i++)
		{
			offsetPos = GlobalUtil.RandomVector3(minPoint, maxPoint);
			pos = basePos + offsetPos;

			go = pool.Get(parent, false);
			go.transform.position = pos;

			InserTree(go);
		}
	}

	void InserTree(GameObject go)
	{
		var node = new AABB();
		node.GO = go;
		Vector3 center = go.transform.position;
		Vector3 size = go.GetComponent<BoxCollider2D>().bounds.size;
		node.Bound = new Bounds(center, size);
		Debug.Log(node.Bound);
		tree.Insert(node);
	}

	public void SetColor(object key, Color color)
	{
		List<object> keys = new List<object>() { key };
		var node = tree.Find(keys);
		List<MultiTreeNode<AABB>> list = tree.GetAllNodeByNode(node);
		foreach (var item in list)
		{
			item.value.GO.GetComponent<SpriteRenderer>().color = color;
		}
	}

	public bool SelectCondition(MultiTreeNode<AABB> node)
	{
		if (bounds.Intersects(node.value.Bound))
			return true;
		return false;
	}

	#region 
	public void OnDrawGizmos()
	{
		if (Camera.current != Camera.main)
			return;

		List<MultiTreeNode<AABB>> list = tree.GetAllNode(false);
		foreach (var item in list)
		{
			if (item.Map.Count == 0) continue;
			Gizmos.color = GlobalUtil.GetColor(item.height);
			Gizmos.DrawWireCube(item.value.Bound.center, item.value.Bound.size);
		}

		if (isMouseDown)
		{
			upPoint = GlobalUtil.GetMouseInPlanePos(plane);
			bounds = new Bounds();
			bounds.center = (upPoint + downPoint) / 2;
			bounds.size = new Vector3(MathF.Abs(upPoint.x - downPoint.x), MathF.Abs(upPoint.y - downPoint.y), 0.01f);
			Color color = Color.green;
			color.a = 0.8f;
			Gizmos.color = color;
			Gizmos.DrawCube(bounds.center, bounds.size);
		}
	}

	#endregion

	#region  系统事件
	public void OnEnable()
	{
		EventManager.Register<MouseDownGE>(OnMouseDownEvent);
		EventManager.Register<MouseUpGE>(OnMouseUpEvent);
	}

	public void OnDestroy()
	{
		EventManager.UnRegisterAll(this);
	}

	public void OnMouseDownEvent(MouseDownGE eventData)
	{
		if (eventData.key == MouseKey.Left)
		{
			isMouseDown = true;
			downPoint = GlobalUtil.GetMouseInPlanePos(plane);
		}
	}

	public void OnMouseUpEvent(MouseUpGE eventData)
	{
		if (eventData.key != MouseKey.Left) return;

		isMouseDown = false;
		upPoint = GlobalUtil.GetMouseInPlanePos(plane);

		bounds = new Bounds();
		bounds.center = (upPoint + downPoint) / 2;
		bounds.size = new Vector3(MathF.Abs(upPoint.x - downPoint.x), MathF.Abs(upPoint.y - downPoint.y), 0);

		foreach (var item in pool.usePool)
		{
			item.GetComponent<SpriteRenderer>().color = UnityEngine.Color.white;
		}

		List<MultiTreeNode<AABB>> list = tree.GetAllNode(SelectCondition);
		foreach (var item in list)
		{
			item.value.GO.GetComponent<SpriteRenderer>().color = UnityEngine.Color.green;
		}
	}

	#endregion
}
